###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################

bazel_dep(name = "abseil-cpp", version = "20250127.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.14.0")
bazel_dep(name = "aspect_rules_lint", version = "1.0.8", dev_dependency = True)
bazel_dep(name = "bazel_skylib", version = "1.7.1", dev_dependency = True)
bazel_dep(name = "boringssl", version = "0.20241024.0")
bazel_dep(name = "brotli", version = "1.1.0")
bazel_dep(name = "crc32c", version = "1.1.0")
bazel_dep(name = "curl", version = "8.8.0.bcr.2")
bazel_dep(name = "fmt", version = "10.2.1")
bazel_dep(name = "freetype", version = "2.13.3")
bazel_dep(name = "gflags", version = "2.2.2")
bazel_dep(name = "googleapis", version = "0.0.0-20241220-5e258e33.bcr.1")
bazel_dep(name = "googleapis-cc", version = "1.0.0")
bazel_dep(name = "googletest", version = "1.15.2")
bazel_dep(name = "grpc", version = "1.71.0")
bazel_dep(name = "grpc-proto", version = "0.0.0-20240627-ec30f58")
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
bazel_dep(name = "icu", version = "76.1.bcr.1")
bazel_dep(name = "jsoncpp", version = "1.9.5")
bazel_dep(name = "libarchive", version = "3.7.9")
bazel_dep(name = "libevent", version = "2.1.12-stable.bcr.0")
bazel_dep(name = "libjpeg_turbo", version = "2.1.91")
bazel_dep(name = "libpng", version = "1.6.47.bcr.1")
bazel_dep(name = "libxml2", version = "2.13.5")
bazel_dep(name = "libzip", version = "1.10.1")
bazel_dep(name = "lz4", version = "1.9.4")
bazel_dep(name = "pcre2", version = "10.45")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "protobuf", version = "29.1")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_flex", version = "0.4")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_proto_grpc_cpp", version = "5.0.1")
bazel_dep(name = "rules_python", version = "0.40.0")
bazel_dep(name = "rules_rust", version = "0.60.0")
bazel_dep(name = "tinyxml2", version = "10.0.0")
bazel_dep(name = "toolchains_llvm", version = "1.2.0")
bazel_dep(name = "sandboxed_api")
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")

# this needs to be at parity with with build_external/crosvm/crosvm.config.toml
CROSVM_REMOTE = "https://chromium.googlesource.com/crosvm/crosvm"
CROSVM_REV = "bc84c8bd6ce078e251594aa1af9e84932c5d0d81"

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
git_override(
    module_name = "hedron_compile_commands",
    # While waiting for
    # https://github.com/hedronvision/bazel-compile-commands-extractor/pull/219
    # to be merged.
    commit = "f5fbd4cee671d8d908f37c83abaf70fba5928fc7",
    remote = "https://github.com/mikael-s-persson/bazel-compile-commands-extractor",
)

git_override(
    module_name = "sandboxed_api",
    commit = "75b6c16e8e95314456795b34af06571879b18e1f",
    remote = "https://github.com/google/sandboxed-api.git",
    patch_strip = 1,
    patches = [
        # See cl/711879539
        "@//build_external/sandboxed_api:PATCH.sandboxed_api.util.visibility.patch",
    ],
)

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.toolchain(
    llvm_version = "18.1.8",
)

use_repo(llvm, "llvm_toolchain")

file_detector = use_repo_rule("//toolchain:file_detector.bzl", "file_detector")

file_detector(
    name = "clang_detector",
    files = {
        "/usr/bin/clang-11": "clang_11",
        "/usr/bin/clang-12": "clang_12",
        "/usr/bin/clang-13": "clang_13",
        "/usr/bin/clang-14": "clang_14",
        "/usr/bin/clang-15": "clang_15",
        "/usr/bin/clang-16": "clang_16",
        "/usr/bin/clang-17": "clang_17",
        "/usr/bin/clang-18": "clang_18",
        "/usr/bin/clang-19": "clang_19",
    },
)

# The first toolchain that satisfies platform constraints is chosen.
# Alternatively, a specific toolchain can be chosen with the
# `--extra_toolchains` flag, e.g.
# ```
# bazel build //cuttlefish/package:cvd \
#   --extra_toolchains=//toolchain:linux_local_clang_19
# ```
#
# For more information, see https://bazel.build/extending/toolchains
register_toolchains(
    "@llvm_toolchain//:all",  # TODO: b/407854179 - demote after testing others
    "//toolchain:linux_local_clang_19",
    "//toolchain:linux_local_clang_18",
    "//toolchain:linux_local_clang_17",
    "//toolchain:linux_local_clang_16",
    "//toolchain:linux_local_clang_15",
    "//toolchain:linux_local_clang_14",
    "//toolchain:linux_local_clang_13",
    "//toolchain:linux_local_clang_12",
    "//toolchain:linux_local_clang_11",
)

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    # Required to avoid an error when running as root e.g. in Docker builds.
    ignore_root_user_error = True,
    python_version = "3.12",
)

append_squashfs_overlay_crates = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
append_squashfs_overlay_crates.from_cargo(
    name = "append_squashfs_overlay_crates",
    cargo_config = "@//build_external/crosvm:crosvm.config.toml",
    manifests = [
        "//cuttlefish/host/commands/append_squashfs_overlay:Cargo.toml",
    ],
    host_tools_repo = "rust_host_tools_nightly",
)
use_repo(append_squashfs_overlay_crates, "append_squashfs_overlay_crates")

vhost_user_input_crates = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
vhost_user_input_crates.from_cargo(
    name = "vhost_user_input_crates",
    cargo_config = "@//build_external/crosvm:crosvm.config.toml",
    manifests = [
        "//cuttlefish/host/commands/vhost_user_input:Cargo.toml",
    ],
    host_tools_repo = "rust_host_tools_nightly",
)
use_repo(vhost_user_input_crates, "vhost_user_input_crates")

git_override(
    module_name = "aspect_rules_lint",
    # While waiting for https://github.com/aspect-build/rules_lint/pull/472 to
    # be merged.
    commit = "62e8aa0e9935bb7db76ead9cc2c1804e4dd740b9",
    remote = "https://github.com/Databean/rules_lint.git",
)

# note that this won't use the same source checkout as the git_repository directive below
#
# The following `annotation` directives modify the generated bazel rules for
# individual crates in the crosvm dependency chart.
#
# See https://bazelbuild.github.io/rules_rust/crate_universe_bzlmod.html
crosvm_bin = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")

crosvm_bin.spec(
    artifact = "bin",
    package = "crosvm",
    features = [
        "android-sparse",
        "android_display",
        "android_display_stub",
        "audio",
        "audio_aaudio",
        "balloon",
        "composite-disk",
        "config-file",
        "fs_runtime_ugid_map",
        "gdb",
        "gdbstub",
        "gdbstub_arch",
        "geniezone",
        "gfxstream",
        "gpu",
        "gpu_display",
        "gunyah",
        "libaaudio_stub",
        "media",
        "net",
        "qcow",
        "usb",
    ],
    # TODO: schuffelen - fix the sandbox
    git = CROSVM_REMOTE,
    rev = CROSVM_REV,
)

crosvm_bin.annotation(
    crate = "crosvm",
    gen_all_binaries = True,
    patch_args = ["-p1"],
    deps = [
        "@@//:gfxstream_backend_import",
    ],
    rustc_flags = [
        # Look for shared libraries in the same directory as the binary.
        "-Clink-arg=-Wl,-rpath,$$ORIGIN",
    ],
)

crosvm_bin.annotation(
    crate = "rutabaga_gfx",
    deps = [
        "@@//:gfxstream_backend_import",
    ],
    rustc_flags = [
        # Look for shared libraries in the same directory as this object.
        "-Clink-arg=-Wl,-rpath,$$ORIGIN",
    ],
    build_script_env = {
        # Disable the build script from trying to automatically locate
        # Gfxstream as the dep is directly provided above.
        "CARGO_FEATURE_GFXSTREAM_STUB": "blah",
    },
)

crosvm_bin.annotation(
    crate = "jail",
    build_script_data_glob = ["seccomp/**"],
    build_script_data = ["@@//:compile_seccomp_policy_exe"],
    build_script_env = {"COMPILE_SECCOMP_POLICY": "$(execpath @@//:compile_seccomp_policy_exe)"},
)

# minijail-sys/build.rs invokes `make` to build the minijail C library, so all
# the sources need to be made available to it at runtime in a filegroup.
# rules_rust only checks out the crate directory and the C sources are two
# directories above, so we grab them from the submodule checkout in our crosvm
# git_repository.
crosvm_bin.annotation(
    crate = "minijail-sys",
    build_script_data = ["@@//:minijail_sources"],
    build_script_rundir = "external/+_repo_rules2+crosvm/third_party/minijail",
    patch_args = ["-p1"],
    # crates_universe checks out only a subdirectory of the repository, so the
    # file paths in this patch are relative to that directory.
    patches = ["@//build_external/crosvm:PATCH.minijail-sys_build_rs.patch"],
)

# Disable the `path` directives for the generated code as they end up with
# temporary paths within the bazel sandbox.
crosvm_bin.annotation(
    crate = "proto_build_tools",
    patch_args = ["-p1"],
    # crates_universe checks out only a subdirectory of the repository, so the
    # file paths in this patch are relative to that directory.
    patches = ["@//build_external/crosvm:PATCH.proto_build_tools.patch"],
)

crosvm_bin.from_specs(
    name = "crosvm_bin",
    cargo_config = "@//build_external/crosvm:crosvm.config.toml",
    cargo_lockfile = "@crosvm//:Cargo.lock",
    # Executable dependencies are an unstable cargo feature.
    host_tools_repo = "rust_host_tools_nightly",
)

use_repo(crosvm_bin, "crosvm_bin")

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Upstream mtools is in subversion, which bazel doesn't support
http_archive(
    name = "mtools",
    build_file = "@//:BUILD.mtools.bazel",
    url = "http://ftp.gnu.org/gnu/mtools/mtools-4.0.48.tar.bz2",
    sha256 = "03c29aac8735dd7154a989fbc29eaf2b506121ae1c3a35cd0bf2a02e94d271a9",
    strip_prefix = "mtools-4.0.48",
)

git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
    name = "fruit",
    build_file = "@//:BUILD.fruit.bazel",
    commit = "0af6ffd113ad963dbd2a23e8cbabba13493c015f", # 2025-02-02
    remote = "https://github.com/google/fruit.git",
)

git_repository(
    name = "egl_headers",
    build_file = "@//:BUILD.egl_headers.bazel",
    commit = "29c4314e0ef04c730992d295f91b76635019fbba",
    remote = "https://github.com/KhronosGroup/EGL-Registry.git",
)

git_repository(
    name = "opengl_headers",
    build_file = "@//:BUILD.opengl_headers.bazel",
    commit = "b53ca669bea4715b6d5fa53c459f47a1fecd7944",
    remote = "https://github.com/KhronosGroup/OpenGL-Registry.git",
)

git_repository(
    name = "vulkan_headers",
    build_file = "@//:BUILD.vulkan_headers.bazel",
    commit = "952f776f6573aafbb62ea717d871cd1d6816c387", # v1.4.309
    remote = "https://github.com/KhronosGroup/Vulkan-Headers.git",
)

git_repository(
    name = "libyuv",
    build_file = "@//:BUILD.libyuv.bazel",
    commit = "af6ac8265bbd07bcf977526458b60305c4304288",
    remote = "https://chromium.googlesource.com/libyuv/libyuv.git",
)

git_repository(
    name = "libffi",
    build_file = "@//:BUILD.libffi.bazel",
    commit = "5dcb741f1544c5e18c9dbf96aeb8b61cc556a616", # v3.3
    remote = "https://github.com/libffi/libffi.git",
)

git_repository(
    name = "spirv_headers",
    commit = "4f7b471f1a66b6d06462cd4ba57628cc0cd087d7",
    remote = "https://github.com/KhronosGroup/SPIRV-Headers.git",
)

git_repository(
    name = "spirv_tools",
    commit = "dd4b663e13c07fea4fbb3f70c1c91c86731099f7", # v2024.2
    remote = "https://github.com/KhronosGroup/SPIRV-Tools.git",
)

git_repository(
    name = "swiftshader",
    build_file = "@//:BUILD.swiftshader.bazel",
    commit = "4c3426403b9c89253322265323ca658a2476faca",
    remote = "https://swiftshader.googlesource.com/SwiftShader.git",
)

git_repository(
    name = "gfxstream",
    commit = "620cd8d0e1e839228d1894da4508d08b8ac798bc",
    remote = "https://github.com/google/gfxstream.git",
)

git_repository(
    name = "libdrm",
    build_file = "@//:BUILD.libdrm.bazel",
    # TODO: b/414448658 - gitlab.freedesktop.org having an outage
    #commit = "998d2a2e81e11043781672494dc3d294c1ecfce0",
    #remote = "https://gitlab.freedesktop.org/mesa/drm.git",
    commit = "f241f822cebbd681e46201b16685a827eb024736",
    remote = "https://android.googlesource.com/platform/external/libdrm.git",
)

git_repository(
    name = "crosvm",
    build_file = "@//:BUILD.crosvm.bazel",
    commit = CROSVM_REV,
    remote = CROSVM_REMOTE,
    init_submodules = True,
    patch_strip = 1,
    patches = ["@//build_external/crosvm:PATCH.minijail-sys_common_mk.patch"],
)

git_repository(
    name = "expat",
    build_file = "@//:BUILD.expat.bazel",
    commit = "2691aff4304a6d7e053199c205620136481b9dd1", # 2.6.4
    remote = "https://github.com/libexpat/libexpat/",
)

git_repository(
    name = "wayland",
    build_file = "@//build_external/wayland:BUILD.wayland.bazel",
    # TODO: b/414448658 - gitlab.freedesktop.org having an outage
    #commit = "b2649cb3ee6bd70828a17e50beb16591e6066288", # 1.22.0
    #remote = "https://gitlab.freedesktop.org/wayland/wayland.git",
    commit = "02aa185fc20e2d9fb0ffee4aaf792ae07addce29",
    remote = "https://android.googlesource.com/platform/external/wayland.git",
    patch_strip = 1,
    patches = [
        "@//build_external/wayland:PATCH.wayland.wayland_config.patch",
    ],
)

git_repository(
    name = "libwebsockets",
    build_file = "@//:BUILD.libwebsockets.bazel",
    commit = "9075f01e1670e64435f4ea501667982fc911ad6b", # v4.3-stable
    remote = "https://github.com/warmcat/libwebsockets",
)

git_repository(
    name = "libcbor",
    commit = "ae000f44e8d2a69e1f72a738f7c0b6b4b7cc4fbf", # v0.12.0
    build_file = "@//:BUILD.libcbor.bazel",
    remote = "https://github.com/PJK/libcbor",
)

git_repository(
    name = "libwebm",
    commit = "6745fd29e0245fc584b0bb9f65018ea2366fe7fb", # libwebm-1.0.0.31
    build_file = "@//:BUILD.libwebm.bazel",
    remote = "https://github.com/webmproject/libwebm",
)

git_repository(
    name = "libsrtp2",
    # This is quite old, but it's what libwebrtc depends on
    commit = "860492290f7d1f25e2bd45da6471bfd4cd4d7add",
    build_file = "@//:BUILD.libsrtp2.bazel",
    remote = "https://github.com/cisco/libsrtp",
)

git_repository(
    name = "libpffft",
    commit = "e0bf595c98ded55cc457a371c1b29c8cab552628",
    build_file = "@//:BUILD.libpffft.bazel",
    remote = "https://github.com/marton78/pffft",
)

git_repository(
    name = "libwebrtc",
    commit = "e093c481bf8fd8e141fee4c007d63da488ce0ef5",
    build_file = "@//:BUILD.libwebrtc.bazel",
    remote = "https://webrtc.googlesource.com/src/",
    patches = ["@//build_external/libwebrtc:PATCH.third_party_libyuv.patch"],
)

git_repository(
    name = "libeigen",
    commit = "3147391d946bb4b6c68edd901f2add6ac1f31f8c",  # 3.4.0
    build_file = "@//:BUILD.libeigen.bazel",
    remote = "https://gitlab.com/libeigen/eigen",
)

git_repository(
    name = "mesa",
    build_file = "@//:BUILD.mesa.bazel",
    commit = "224e91e39836d3ca31f80df2e0379e55c70574f8",
    remote = "https://android.googlesource.com/platform/external/mesa3d",
)

git_repository(
    name = "mako",
    build_file = "@//:BUILD.mako.bazel",
    commit = "51505647100b7cf20a79b496bc12dec379ac9cdc", # 1.3.0
    remote = "https://github.com/sqlalchemy/mako",
)

git_repository(
    name = "markupsafe",
    build_file = "@//:BUILD.markupsafe.bazel",
    commit = "63efa5521c5c75d40cfed0b6b8f6528ecce9bc6a", # 2.0.1
    remote = "https://github.com/pallets/markupsafe",
)

git_repository(
    name = "pyyaml",
    build_file = "@//:BUILD.pyyaml.bazel",
    commit = "41309b0bcb4559edb1d691d47199035ef539d785", # 6.0.2
    remote = "https://github.com/yaml/pyyaml.git",
)

git_repository(
    name = "avb",
    build_file = "@//:BUILD.avb.bazel",
    commit = "761178607206f4cb2af79ed9eec52d8cbd814adb",
    remote = "https://android.googlesource.com/platform/external/avb",
)

git_repository(
    name = "fec_rs",
    build_file = "@//:BUILD.fec_rs.bazel",
    commit = "90857deb7973c0ca24c79b9c1809fc9667f32c4f",
    remote = "https://android.googlesource.com/platform/external/fec",
)

git_repository(
    name = "mkbootimg",
    build_file = "@//:BUILD.mkbootimg.bazel",
    commit = "d2bb0af5ba6d3198a3e99529c97eda1be0b5a093",
    remote = "https://android.googlesource.com/platform/system/tools/mkbootimg",
)

git_repository(
    name = "wmediumd",
    build_file = "@//:BUILD.wmediumd.bazel",
    commit = "f44d2af28b8045eb31ff045d3c078e60865afa98",
    remote = "https://android.googlesource.com/platform/external/wmediumd",
)

git_repository(
    name = "libconfig",
    build_file = "@//:BUILD.libconfig.bazel",
    commit = "81fe4632f01cbf24bf7efcd7ce1913f441c5ea96",
    remote = "https://github.com/hyperrealm/libconfig",
)

# Upstream uses autotools, for now use the Android copy
git_repository(
    name = "libnl",
    build_file = "@//:BUILD.libnl.bazel",
    commit = "f9682564a48d5ac5ee922de49e81aab4aa984f1e",
    remote = "https://android.googlesource.com/platform/external/libnl",
)

# Upstream uses autotools, for now use the Android copy
git_repository(
    name = "libusb",
    build_file = "@//:BUILD.libusb.bazel",
    commit = "7b5267099855d320f6013d667d113e3619f18bf5",
    remote = "https://android.googlesource.com/platform/external/libusb",
)

# Android version is slightly forked from the chromium upstream
git_repository(
    name = "libopenscreen",
    build_file = "@//:BUILD.libopenscreen.bazel",
    commit = "3f982cf4871df8771c9d4abe6e9a6f8d829b2736",
    remote = "https://android.googlesource.com/platform/external/openscreen",
    patch_strip = 1,
    patches = [
        "@//build_external/libopenscreen:PATCH.bazelignore.patch",
    ],
)

git_repository(
    name = "libvpx",
    build_file = "@//:BUILD.libvpx.bazel",
    commit = "39e8b9dcd4696d9ac3ebd4722e012488382f1adb", # v1.15.1
    remote = "https://chromium.googlesource.com/webm/libvpx",
)

# Android version is forked from github upstream
git_repository(
    name = "selinux",
    build_file = "@//:BUILD.selinux.bazel",
    commit = "6f8f496eabd35f28dfa6b4be5fc82e325e1df620",
    remote = "https://android.googlesource.com/platform/external/selinux",
)

git_repository(
    name = "e2fsprogs",
    build_file = "@//:BUILD.e2fsprogs.bazel",
    commit = "ff64357f839071968a727f8b5a08b0ddaedc5bbb",
    remote = "https://github.com/tytso/e2fsprogs",
)

git_repository(
    name = "f2fs_tools",
    build_file = "@//:BUILD.f2fs_tools.bazel",
    commit = "d8eac1f8699541416afdf93333772ef2e0509773",
    remote = "https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs-tools",
)

git_repository(
    name = "dosfstools",
    build_file = "@//:BUILD.dosfstools.bazel",
    commit = "289a48b9cb5b3c589391d28aa2515c325c932c7a",
    remote = "https://github.com/dosfstools/dosfstools",
    patch_strip = 1,
    patches = [
        "@//build_external/dosfstools:PATCH.vasprintf.patch",
    ],
)
